﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;

using System.IO;
using System.Text;
using Yipusoft.TownshipEnterprise.BLL;

public partial class TownshipEnterprise_VehicleStat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }
    HtmlTableCell td;
    HtmlTableRow tr;
    void init()
    {

        //所有的部门
        List<DepartmentInfo> departments = Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
        //执勤车辆

        string strXFC = " Groupid='XFZBLB' and Parentid='10' AND ItemID <> '1060'";

        //非执勤车辆
        string strFXFC = " Groupid='XFZBLB' and Parentid='10'  AND (ItemID = '1060') ";
        //头部分
        tr = new HtmlTableRow();
        tr.Cells.Add(new HtmlTableCell());

        tr.Cells[0].InnerText = "车辆类型";
        tr.Cells[0].Attributes.Add("colspan", "2");

        tr.Cells[0].Attributes.Add("align", "center");
        tr.Cells[0].Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse;  border-width: 1px; border-style:solid;");
        for (int i = 0; i < departments.Count; i++)
        {
            td = new HtmlTableCell();
            td.InnerText = departments[i].AliasName;
            td.Attributes.Add("align", "center");
            td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse;  border-width: 1px; border-style:solid;");
            tr.Cells.Add(td);
        }
        table1.Rows.Add(tr);
        //显示消防车
        ViewXFC(departments, strXFC);
        //显示非执勤消防车
        ViewXFC(departments, strFXFC);
    }

    private void ViewXFC(List<DepartmentInfo> departments, string where)
    {
        List<CodeItemInfo> XFC = CodeItem.GetList2(where);
        string strXFC = "";
        for (int j = 0; j < XFC.Count; j++)
        {
            List<CodeItemInfo> XFC00 = CodeItem.GetList2(" parentID='" + XFC[j].ItemID + "' ");
            for (int k = 0; k < XFC00.Count; k++)
            {
                strXFC += "'" + XFC00[k].ItemID + "',";
                tr = new HtmlTableRow();
                if (k == 0)
                {
                    td = new HtmlTableCell();
                    td.InnerText = XFC[j].ItemName;
                    td.Attributes.Add("rowspan", XFC00.Count.ToString());
                    td.Attributes.Add("colspan", "1");

                    td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse;  border-width: 1px; border-style:solid;");
                    tr.Cells.Add(td);
                }
                td = new HtmlTableCell();
                td.InnerText = XFC00[k].ItemName;

                td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(td);
                for (int i = 0; i < departments.Count; i++)
                {
                    td = new HtmlTableCell();
                    string departmentid = departments[i].DepartmentID;
                    string VehicleClass = XFC00[k].ItemID;
                    string Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and  VehicleClass='" + XFC00[k].ItemID + "'";
                    int count = TownVehicles.GetCountTownbasicRightVehicles(Where);

                    HtmlAnchor anchor = new HtmlAnchor();
                    // anchor.Title = count.ToString();
                    anchor.InnerText = count.ToString();
                    anchor.HRef = "VehicleList.aspx?Where=" + Where + " ";
                    td.Controls.Add(anchor);
                    td.Attributes.Add("align", "center");
                    td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse;  border-width: 1px; border-style:solid;");

                    tr.Cells.Add(td);
                }
                table1.Rows.Add(tr);

            }


        }
        if (strXFC.Length > 0)
        {
            strXFC = strXFC.Remove(strXFC.Length - 1);
            strXFC = "VehicleClass in (" + strXFC + ")";
        }
        //总计部分
        tr = new HtmlTableRow();
        HtmlTableCell totolTd = new HtmlTableCell();
        totolTd.InnerText = "总计";
        totolTd.ColSpan = 2;
        tr.Cells.Add(totolTd);
        for (int i = 0; i < departments.Count; i++)
        {
            td = new HtmlTableCell();
            string departmentid = departments[i].DepartmentID;
            //string VehicleClass = XFC00[k].ItemID;
            string Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and " + strXFC;
            int count = TownVehicles.GetCountTownbasicRightVehicles(Where);

            HtmlAnchor anchor = new HtmlAnchor();
            anchor.Title = count.ToString();
            anchor.InnerText = count.ToString();
            anchor.HRef = "VehicleList.aspx?Where=" + Where + " ";
            td.Controls.Add(anchor);
            td.Attributes.Add("align", "center");
            td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse;  border-width: 1px; border-style:solid;");

            tr.Cells.Add(td);
        }
        table1.Rows.Add(tr);
        table1.Attributes.Add("style", "border-color: #CEE7F7;border-collapse: collapse;  border-width: 1px; border-style:solid;");
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        ExportBind();
        ExportData("application/ms-excel", DateTime.Now.ToString("yyMdHm") + ".xls");
    }

    private void ExportData(string FileType, string FileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        table1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

    void ExportBind()
    {

        //所有的部门
        List<DepartmentInfo> departments = Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
        //执勤车辆

        string strXFC = " Groupid='XFZBLB' and Parentid='10' AND ItemID <> '1060'";

        //非执勤车辆
        string strFXFC = " Groupid='XFZBLB' and Parentid='10'  AND (ItemID = '1060') ";
        //头部分
        tr = new HtmlTableRow();
        tr.Cells.Add(new HtmlTableCell());

        tr.Cells[0].InnerText = "车辆类型";
        tr.Cells[0].Attributes.Add("colspan", "2");

        tr.Cells[0].Attributes.Add("align", "center");
        tr.Cells[0].Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse;  border-width: 1px; border-style:solid;");
        for (int i = 0; i < departments.Count; i++)
        {
            td = new HtmlTableCell();
            td.InnerText = departments[i].AliasName;
            td.Attributes.Add("align", "center");
            td.Attributes.Add("style", "border-collapse: collapse;  border-width: 1px; border-style:solid;");
            tr.Cells.Add(td);
        }
        table1.Rows.Add(tr);
        //显示消防车
        ViewXFC1(departments, strXFC);
        //显示非执勤消防车
        ViewXFC1(departments, strFXFC);

    }
    private void ViewXFC1(List<DepartmentInfo> departments, string where)
    {
        List<CodeItemInfo> XFC = CodeItem.GetList2(where);
        string strXFC = "";
        for (int j = 0; j < XFC.Count; j++)
        {
            List<CodeItemInfo> XFC00 = CodeItem.GetList2(" parentID='" + XFC[j].ItemID + "' ");
            for (int k = 0; k < XFC00.Count; k++)
            {
                strXFC += "'" + XFC00[k].ItemID + "',";
                tr = new HtmlTableRow();
                if (k == 0)
                {
                    td = new HtmlTableCell();
                    td.InnerText = XFC[j].ItemName;
                    td.Attributes.Add("rowspan", XFC00.Count.ToString());
                    td.Attributes.Add("colspan", "1");

                    td.Attributes.Add("style", "border-collapse: collapse;  border-width: 1px; border-style:solid;");
                    tr.Cells.Add(td);
                }
                td = new HtmlTableCell();
                td.InnerText = XFC00[k].ItemName;

                td.Attributes.Add("style", "border-collapse: collapse;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(td);
                for (int i = 0; i < departments.Count; i++)
                {
                    td = new HtmlTableCell();
                    string departmentid = departments[i].DepartmentID;
                    string VehicleClass = XFC00[k].ItemID;
                    string Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and  VehicleClass='" + XFC00[k].ItemID + "'";
                    int count = TownVehicles.GetCountTownbasicRightVehicles(Where);

                    HtmlAnchor anchor = new HtmlAnchor();

                    anchor.InnerText = count.ToString();

                    td.Controls.Add(anchor);
                    td.Attributes.Add("align", "center");
                    td.Attributes.Add("style", "border-collapse: collapse;  border-width: 1px; border-style:solid;");

                    tr.Cells.Add(td);
                }
                table1.Rows.Add(tr);

            }


        }
        if (strXFC.Length > 0)
        {
            strXFC = strXFC.Remove(strXFC.Length - 1);
            strXFC = "VehicleClass in (" + strXFC + ")";
        }
        //总计部分
        tr = new HtmlTableRow();
        HtmlTableCell totolTd = new HtmlTableCell();
        totolTd.InnerText = "总计";
        totolTd.ColSpan = 2;
        tr.Cells.Add(totolTd);
        for (int i = 0; i < departments.Count; i++)
        {
            td = new HtmlTableCell();
            string departmentid = departments[i].DepartmentID;

            string Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and " + strXFC;
            int count = TownVehicles.GetCountTownbasicRightVehicles(Where);

            HtmlAnchor anchor = new HtmlAnchor();
            anchor.Title = count.ToString();
            anchor.InnerText = count.ToString();

            td.Controls.Add(anchor);
            td.Attributes.Add("align", "center");
            td.Attributes.Add("style", "border-collapse: collapse;  border-width: 1px; border-style:solid;");

            tr.Cells.Add(td);
        }
        table1.Rows.Add(tr);
        table1.Attributes.Add("style", "border-collapse: collapse;  border-width: 1px; border-style:solid;");
    }
}
